//package com.platform.interceptor;
//
//import cn.hutool.json.JSONUtil;
//import com.platform.bean.JwtProperties;
//import com.platform.exception.BadRequestException;
//import com.platform.exception.TokenExpiredException;
//import com.platform.utils.JwtTokenUtils;
//import com.platform.utils.RedisUtils;
//import io.jsonwebtoken.io.Decoders;
//import io.jsonwebtoken.security.Keys;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.annotation.Resource;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.security.Key;
//import java.util.concurrent.TimeUnit;
//
//@Component
//public class LoginInterception implements HandlerInterceptor {
//
//    @Resource
//    private RedisTemplate<String, Object> redisTemplate;
//    @Resource
//    private JwtProperties jwtProperties;
//    @Resource
//    private RedisUtils redisUtils;
//    @Resource
//    private JwtTokenUtils jwtTokenUtils;
//
////    @Override
////    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
////        // 获取token
////        String token = request.getHeader(Constant.TOKEN_HEADER_NAME);
////        if (StringUtils.isBlank(token)) {
////            returnNoLogin(response);
////            return false;
////        }
////        // 从redis中拿token对应user
////        User user = (User) redisTemplate.opsForValue().get(Constant.REDIS_USER_PREFIX + token);
////        if (user == null) {
////            returnNoLogin(response);
////            return false;
////        }
////        // token续期
////        redisTemplate.expire(Constant.REDIS_USER_PREFIX + token, 30, TimeUnit.MINUTES);
////        // 放行
////        return true;
////    }
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token = request.getHeader("Authorization");
//        // todo 区分用户和管理员
//        Object identity = request.getAttribute("identity");
//        String requestURI = request.getRequestURI();
//        System.out.println("请求uri " + requestURI);
//        System.out.println("请求urL" + request.getRequestURL());
//        // TODO 针对get请求中传递username的情况，把username和token进行比较，看看是否对的上
//
//        if (token != null && !StringUtils.isBlank(token)) {
//            byte[] keyBytes = Decoders.BASE64.decode(jwtProperties.getBase64Secret());
//            Key key = Keys.hmacShaKeyFor(keyBytes);
//            token = token.substring(7);
////        Claims claims = Jwts.parser()
////                .setSigningKey(keyBytes) // 使用你的原始密钥字节数组
////                .parseClaimsJws(token)
////                .getBody();
////
////        // 从这里开始，你可以从claims中获取你需要的信息，比如用户名等
////        String username2 = claims.getSubject();
////        System.out.println("username2" + username2);
//            System.out.println("获取到的token：" + token);
//            if (StringUtils.isBlank(token)) {
//                throw new BadRequestException("token为空");
//            }
//            if ("1".equals(identity)) {
//                if (redisUtils.get(jwtTokenUtils.loginUserKey(token)) == null || StringUtils.isBlank((String)redisUtils.get(jwtTokenUtils.loginUserKey(token)))) {
//                    throw new TokenExpiredException("登录状态过期，请重新登录");
//                }
//                redisUtils.updateKeyTtl(jwtTokenUtils.loginUserKey(token),jwtProperties.getTokenValidityInSeconds(), TimeUnit.MILLISECONDS);
//            } else {
//                System.out.println("处理用户登录");
//            }
//        }
//        return true;
//    }
//
////    /**
////     * 返回未登录的错误信息
////     * @param response ServletResponse
////     */
////    private void returnNoLogin(HttpServletResponse response) throws IOException {
////        ServletOutputStream outputStream = response.getOutputStream();
////        // 设置返回401 和响应编码
////        response.setStatus(401);
////        response.setContentType("Application/json;charset=utf-8");
////        // 构造返回响应体
////        Result<String> result = Result.<String>builder()
////                .code(HttpStatus.UNAUTHORIZED.value())
////                .errorMsg("未登陆，请先登陆")
////                .build();
////        String resultString = JSONUtil.toJsonStr(result);
////        outputStream.write(resultString.getBytes(StandardCharsets.UTF_8));
////    }
//
//}
